﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.GUI.Users.Common;
using Portal.User.;
using Portal.User.Security;
using Portal.GUI.Administrator.BoxManagement.Business;
//using Xmobile.Users;

namespace Portal.GUI.Administrator.BoxManagement
{
    public partial class PerBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.RegisterHiddenField("hidIdPrefix", ClientID + "_");
            cmdSave.Attributes.Add("onclick", "return Validate();");
            LoadChannel();
            LoadBox();
            LoadPermission();
            if (!Page.IsPostBack)
            {
                //string id = Request["id"];
                string id = Session["Edit_UserID"] != null ? Session["Edit_UserID"].ToString() : "";
                ViewState["originalId"] = id;
                //string id = Session["Edit_UserID"] != null ? Session["Edit_UserID"].ToString() : "";
                /////////
                //Session["BP_ID"] = "2";
                string aa = Session["BP_ID"] != null ? Session["BP_ID"].ToString() : "";
                if (aa != null && aa.Length > 0)
                {
                    Session["status"] = "update";
                    lblTitle.Text = "Sửa quyền cho box";
                    DataBindBoxPer(aa);
                }
                else
                {
                    Session["status"] = "New";
                    lblTitle.Text = "Thêm mới quyền cho box";
                }
                /////////////
                
            } 
            else
            {

            }
        }
        #region Load Combo
        private void LoadChannel()
        {  
            ddlChannel.DataTextField = "Channel_Name";
            ddlChannel.DataValueField = "Channel_ID";
            MainSecurity ms = new MainSecurity();
            using (MainDB db = new MainDB())
            {
                ddlChannel.DataSource = db.ChannelCollection.GetAllAsDataTable();
            }
            ddlChannel.DataBind();
        }
        private void LoadBox()
        {
            ddlBox.DataTextField = "Box_Name";
            ddlBox.DataValueField = "Box_ID";
            MainSecurity ms = new MainSecurity();
            using (MainDB db = new MainDB())
            {
                ddlBox.DataSource= db.Box_Permission.GetAllAsDataTable();
            }
            ddlBox.DataBind();
        }
        private void LoadPermission()
        {
            ddlPermission.DataTextField = "Permission_Name";
            ddlPermission.DataValueField = "Permission_ID";
            MainSecurity ms = new MainSecurity();
            using (MainDB db = new MainDB())
            {
                ddlPermission.DataSource = db.Box_Permission.GetAllAsDataTablePer();
            }
            ddlPermission.DataBind();
        }
        #endregion
        private void DataBindBoxPer(string BP_ID)
        {
            int a = Convert.ToInt32(BP_ID);
            //DataRow iRow = clsHRContract.GetCurrentInfoByEmpID(a);
            DataRow iRow = Box.GetDataRow(a);//hàm lấy DL
            if (iRow == null) return;
            try
            {
                ddlBox.SelectedValue = iRow["Box_ID"].ToString().Trim();
                ddlChannel.SelectedValue = iRow["Channel_ID"].ToString().Trim();
                ddlPermission.SelectedValue = iRow["Permission_ID"].ToString().Trim();
                txtIndex.Text = iRow["BP_Index"].ToString().Trim();
                txtUrl.Text = iRow["BP_Url"].ToString().Trim();
                txtControl.Text = iRow["BP_Control"].ToString().Trim();

                if (iRow["BP_IsCount"].ToString().Trim() == "True")
                {
                    ChkIscount.Checked = true;
                }
                else
                {
                    ChkIscount.Checked = false;
                }
                if (iRow["BP_IsPrefix"].ToString().Trim() == "True")
                {
                    ChkIsprefix.Checked = true;
                }
                else
                {
                    ChkIsprefix.Checked = false;
                }

            }
            catch (Exception ex)
            {
                //lblErr.Text = ex.Message;
                //clsChangeLang.popupWindowExp(this.Parent,ex);
            }
        }

        #region Save and Cancel button event handlers
        private void OnCancelClick(object sender, EventArgs e)
        {
            Session["BP_ID"] = "";
            Response.Redirect("/box/perlist.chn");
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            int Box = Int32.Parse(ddlBox.SelectedValue);
            int Per = Int32.Parse(ddlPermission.SelectedValue);
            int Channel = Int32.Parse(ddlChannel.SelectedValue);
            int index = Convert.ToInt32(txtIndex.Text.ToString());
            string Url = txtUrl.Text.ToString();
            string strurl = " N'" + Url + "' ";
            string Control = txtControl.Text.ToString();
            string strControl = " N'" + Control + "' ";
            int Iscount;
            int Isprefix;
            if (ChkIscount.Checked == true)
            {
                Iscount = 1;
            }
            else
            {
                Iscount = 0;
            }
            if (ChkIsprefix.Checked == true)
            {
                Isprefix = 1;
            }
            else
            {
                Isprefix = 0;
            }
            ////thao tác DL
            if (Session["status"] == "New")//tạo mới
            {
                Boolean c = Business.Box.InsertBox_Per(Box, Per, index, Url, Control, Iscount, Isprefix, Channel);
            }
            else // sửa
            {
                string x = Session["BP_ID"].ToString();
                int BP_ID = Convert.ToInt32(x.ToString());
                Boolean c = Business.Box.UpdateBox_Per(BP_ID,Box, Per, index, Url, Control, Iscount, Isprefix, Channel);
            }
            //ClearValue();
            Session["BP_ID"] = "";
            Response.Redirect("/box/perlist.chn");

        }
        #endregion
        private void ClearValue()
        {
            txtControl.Text = "";
            txtUrl.Text = "";
            txtIndex.Text = "";
            ChkIscount.Checked = false;
            ChkIsprefix.Checked = false;
        }
    }
}